#include <psptypes.h>
#include <pspgu.h>

#include "loadmap.cpp"
#include "lvlsplashtex.h"

ScePspFVector3 mvblk = {0, 0, 0};

int levelsplash = 0;
int loadedmap = 0;
bool waitsplash;
bool waitgameover;
bool showbar = false;
int Polygons = 0;

void mcShowLevelSplash(void)
{
    waitsplash = true;
    Monkey.LockMove = true;
    
    sceGumPushMatrix();
    ScePspFVector3 bgvec = { Monkey.Cam.x-1.64f, 0, 0.2f };
    sceGumTranslate(&bgvec);
    
    sceGuDisable(GU_TEXTURE_2D);
    sceGumDrawArray(GU_LINE_STRIP, GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                    lvlvtxc[Monkey.Level-1], 0, vlvl[Monkey.Level-1]);
    sceGuEnable(GU_TEXTURE_2D);
    sceGumPopMatrix();
    
    CheckArea();
    UpdateCoords();
}

void GameOver()
{
    waitgameover = true;
    sceGumPushMatrix();
    ScePspFVector3 bgvec = { Monkey.Cam.x-1.64f, 0, 0.2f };
    sceGumTranslate(&bgvec);
    
    sceGuDisable(GU_TEXTURE_2D);
    
    for(int i = 0; i < 11; i++)
    {
        sceGumDrawArray(GU_LINE_STRIP, GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                        gameover_vtx[i], 0, gameover[i]);
    }
    
    sceGuEnable(GU_TEXTURE_2D);
    sceGumPopMatrix();
    
    CheckArea();
    UpdateCoords();
}

void mcShowBar(void)
{
    pspDebugScreenSetOffset((int)fbp0);
    pspDebugScreenSetXY(3, 0);

    char lvlsb[3]; sprintf(lvlsb,"%d", levelsplash);
    char pointsb[7]; sprintf(pointsb,"%d", Monkey.Points);
    char livesb[3] = ""; sprintf(livesb,"%d", Monkey.Lives);
    char bananasb[4] = ""; sprintf(bananasb,"%d", Monkey.Bananas);

    if(levelsplash < 10)
        sprintf(lvlsb,"0%d",levelsplash);
    if(Monkey.Lives < 10)
        sprintf(livesb,"0%d", Monkey.Lives);
    if(Monkey.Bananas < 10)
        sprintf(bananasb,"00%d", Monkey.Bananas);
    else if(Monkey.Bananas < 100)
        sprintf(bananasb,"0%d", Monkey.Bananas);
    if(Monkey.Points < 10)
        sprintf(pointsb,"000000%d", Monkey.Points);
    else if(Monkey.Points < 100)
        sprintf(pointsb,"00000%d", Monkey.Points);
    else if(Monkey.Points < 1000)
        sprintf(pointsb,"0000%d", Monkey.Points);
    else if(Monkey.Points < 10000)
        sprintf(pointsb,"000%d", Monkey.Points);
    else if(Monkey.Points < 100000)
        sprintf(pointsb,"00%d", Monkey.Points);
    else if(Monkey.Points < 1000000)
        sprintf(pointsb,"0%d", Monkey.Points);
    
    pspDebugScreenPrintf("Level: %s  ::  Points: %s  ::  Lives: %s  ::  Bananas: %s", lvlsb, pointsb, livesb, bananasb);
}

void DrawBackground()
{
    ScePspFVector3 bgvec = { Monkey.Cam.x, 0, 0 };
    sceGumPushMatrix();
    
    sceGumTranslate(&bgvec);
    sceGuTexImage(0, 256, 512, spr_misc.Width(), spr_misc.Image());
    sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                4, 0, bgv[Rmap.bgtex]);
    
    //STATSBAR BG
    sceGuDisable(GU_TEXTURE_2D);
    sceGumDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                4, 0, sBar);
    sceGuEnable(GU_TEXTURE_2D);
    //END
    
    sceGumPopMatrix();
    Polygons++;
}

void DrawBlocks()
{
    for(int i = 0; i < blkid; i++)
    {
        if(!IsRightArea(Rblk[i].area)) 
            continue;
        
        Polygons++;
        sceGumPushMatrix();
        
        mvblk.x = Rblk[i].x;
        mvblk.y = Rblk[i].y;
        sceGumTranslate(&mvblk);

        sceGuTexImage(0, 256, 512, spr_misc.Width(), spr_misc.Image());
        sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                    4, 0, blkv[Rblk[i].blk]);
        
        sceGumPopMatrix();
        
        //MOVING-BRUSHES
        /*if(Rblk[i].master != 0)
        {
            int master = Rblk[i].master;
            
            if(Rmb[master].dir == 0) //LEFT-RIGHT
            {
                if(Rmb[master].akdir == 0) //RIGHT
                    Rblk[i].x += 0.7f * dt * Rmb[master].speed;
                else
                    Rblk[i].x -= 0.7f * dt * Rmb[master].speed; 
            } else {
                
            }
            
            Rblk[i].area = GetArea(Rblk[i].x);
        }*/
    }
}

/*void MovingBrushes()
{
    for(int i = 0; i < mbid; i++)
    {
        if(Rmb[i].dir == 0) //LEFT-RIGHT
        {
            if(Rmb[i].akdir == 0) //RIGHT
            {
                Rmb[i].x += 0.7f * dt * Rmb[i].speed;
                if(Rmb[i].x > Rmb[i].max_high)
                    Rmb[i].akdir = 1;
            } else {
                Rmb[i].x -= 0.7f * dt * Rmb[i].speed;
                if(Rmb[i].x < Rmb[i].max_low)
                    Rmb[i].akdir = 0;
            }
        }
    }
}*/

void DrawBananas()
{
    sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
    sceGuEnable(GU_BLEND);
    sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);

    for(int i = 0; i < coinid; i++)
    {
        if(Rcon[i].x == -999 || !IsRightArea(Rcon[i].area) || (Rcon[i].x == 0 && Rcon[i].y == 0))
            continue;
        
        Polygons++;
        sceGumPushMatrix();

        mvblk.x = Rcon[i].x;
        mvblk.y = Rcon[i].y;
        sceGumTranslate(&mvblk);

        sceGuTexImage(0, 256, 512, spr_misc.Width(), spr_misc.Image());
        sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                    4, 0, bananav);
        
        sceGumPopMatrix();
    }
}

void DrawMonkey()
{
    sceGumPushMatrix();
    sceGumTranslate(&Monkey.Pos);
    
    if(Monkey.Death)
        sceGumRotateZ(48);
    
    sceGuTexImage(0, 256, 32, spr_monkey.Width(), spr_monkey.Image());
    sceGumDrawArray(GU_TRIANGLE_STRIP, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
                4, 0, monkeyv[Monkey.State]);

    sceGumPopMatrix();
    
    sceGuDisable(GU_BLEND);
    sceGuTexFunc(GU_TFX_DECAL, GU_TCC_RGB);
    Polygons++;
}

void DrawMap(void)
{
    Polygons = 0;
    //RESET VECTOR
    mvblk.x = 0;
    mvblk.y = 0;
    mvblk.z = 0;
    
    /*################### Texture Settings ###################*/
    sceGuTexFunc(GU_TFX_DECAL, GU_TCC_RGB);
    sceGuTexMode(GU_PSM_8888, 0, 0, false);
    sceGumTranslate(&translate);
    
    if(!IsDeath())
    {
        Gravity();
        CheckBanana();
    }
    
    if(!Monkey.LockMove)
        CheckButtons();
    
    CheckArea();
    UpdateCoords();
    
    DrawBackground();
    DrawBlocks();
    //MovingBrushes();
    DrawBananas();
    DrawMonkey();

    sceGuTexMode(GU_PSM_8888, 0, 0, true);
    Monkey.Points = Polygons;
}

void ShowLevel(void)
{
    if(loadedmap != Monkey.Level)
    {
        mcLoadMap(Monkey.Level);
        loadedmap = Monkey.Level;
    }

    sceGumTranslate(&translate);

    if(levelsplash != Monkey.Level)
        mcShowLevelSplash();
    else if(Monkey.Lives == -1)
        GameOver();
    else
        DrawMap();
}

void BananaDebug()
{
    if(Monkey.Bananas < 555)
        Monkey.Bananas++;
}